home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: prefix vs. postfix
- Date: 3 Jan 1996 20:49 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <3JAN199620494722@erich.triumf.ca>
- References: <4c1089$gro@nntpd2.cxo.dec.com> <tcpnntpd.15.12.30.11.27.18.2781597121.305087@the-fix.sos.on.ca> <4cf35v$7il@head.globalcom.net>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4cf35v$7il@head.globalcom.net>, scicom@globalcom.net (stephen j beaver) writes...
- ><SNIP>
- >
- >>The two operators _never_ do the same thing. However, if you just want the
- >>side effect of the increment, but the value returned by i++ or ++i is not used,
- >>(as in for(i = 0; i < 10; i++) ) it doesn't matter which is used.
- >
- >I thought that:
- >
- >for(i=0;i<10;i++)
- > {
- > loopbody(i):
- > }
- >
- >Increments i after the loop so that the first value passed to loopbody() is zero, whereas:
- >
- >for(i=0;i<10;++i)
- > {
- > loopbody();
- > }
- >
- >increments i before the loop body so that the first value passedtp loopbody() is 1 in this case.
- >
- >No?
-
- NO!
-
- The "i++" (or "++i") can be thought of as occuring after the body of the loop,
- just before the program jumps back to the top of the loop.
-
- Writing this loop:
- for(i = 0; i < 10; i++)
- {
- loopbody(i);
- }
- with gotos gives something like this:
- i = 0;
- loop:
- if(!(i < 10)) goto end
- loopbody(i);
- i++;
- goto loop;
- end:
- carry on...
- so whether you use i++ or ++i is irrelevant - the first time through the loop,
- i will be 0.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-
-
-
-
-
-
-
-